#include "ringQueue.hpp"
#include <cstdlib>
#include <ctime>
#include <sys/types.h>
#include <unistd.h>

void *consumer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (1)
    {
        sleep(1);
        int x;
        rq->pop(&x);
        std::cout << "消费: " << x << "[" << pthread_self() << "]" << std::endl;
    }
}
void *producer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;
    while (1)
    {
        //sleep(1);
        int x = rand() % 100 + 1;
        std::cout << "生产: " << x << "[" << pthread_self() << "]" << std::endl;
        rq->push(x);
    }
}
int main()
{
    srand((unsigned)time(nullptr) ^ getpid());
    RingQueue<int> *rq = new RingQueue<int>();
    // pthread_t p, c;
    // pthread_create(&c, nullptr, consumer, (void *)rq);
    // pthread_create(&p, nullptr, producer, (void *)rq);

    // pthread_join(p, nullptr);
    // pthread_join(c, nullptr);

    pthread_t c[3], p[2];
    pthread_create(c, nullptr, consumer, (void *)rq);
    pthread_create(c + 1, nullptr, consumer, (void *)rq);
    pthread_create(c + 2, nullptr, consumer, (void *)rq);
    pthread_create(p, nullptr, producer, (void *)rq);
    pthread_create(p + 1, nullptr, producer, (void *)rq);

    for (int i = 0; i < 3; i++)
        pthread_join(c[i], nullptr);
    for (int i = 0; i < 2; i++)
        pthread_join(p[i], nullptr);

    return 0;
}